package weaver.jobs.jdjs.duss.cron;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.jdjs.duss.utils.MryebUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-08-29
 * @Description: 每天定时计算昨天的余额表
 * @ModifyLog:
 **/

public class WeaMryebCron extends BaseCronJob {

    @Override
    public void execute() {
        try {
            RecordSet rs = new RecordSet();
            String yestDate = DateUtil.addDate(DateUtil.getCurrentDate(), -1);
            String currentDate = DateUtil.getCurrentDate();
            rs.writeLog(">>>>>WeaMryebCron>>>>>yestDate=" + yestDate + ",currentDate=" + currentDate);
            rs.executeQuery("select id,ztgs,gszh,zhxz,ye,rq,tbrq from uf_gsztzhzjcshb");
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String ztgs = Util.null2String(rs.getString("ztgs"));
                String gszh = Util.null2String(rs.getString("gszh"));
                String zhxz = Util.null2String(rs.getString("zhxz"));
                String ye = Util.null2String(rs.getString("ye"));
                String rq = Util.null2String(rs.getString("rq"));
                String tbrq = Util.null2String(rs.getString("tbrq"));
                if ("".equals(tbrq)) {//初始化余额
                    List<String> dateList = MryebUtil.printDatesBetween(rq, yestDate);
                    for (String datestr : dateList) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("rq", datestr);
                        map.put("ztgs", ztgs);
                        map.put("gszh", gszh);
                        map.put("zhxz", zhxz);
                        String zrye = MryebUtil.getMrsjztye(ztgs, gszh, DateUtil.addDate(datestr,-1));
                        if("".equals(zrye)){
                            map.put("ye", MryebUtil.isZero(ye));
                            map.put("ztye", MryebUtil.isZero(ye));
                            map.put("srje", "0");
                            map.put("zcje", "0");
                        }else {
                            Map<String, String> mryeMap = MryebUtil.getMrsjye(ztgs, gszh, datestr);//今日收入-今日支出
                            String srje = Util.null2String(mryeMap.get("srje"));
                            String zcje = Util.null2String(mryeMap.get("zcje"));
                            String jrje = MryebUtil.bSubtract(srje, zcje);
                            map.put("ye", MryebUtil.bAdd(jrje, zrye));
                            map.put("ztye", MryebUtil.isZero(zrye));
                            map.put("srje", MryebUtil.isZero(srje));
                            map.put("zcje", MryebUtil.isZero(zcje));
                        }
                        MryebUtil.insertModelData("uf_mryeb", map);
                    }
                    String updateSql = "update uf_gsztzhzjcshb set tbrq=? where id=?";
                    rs.executeUpdate(updateSql, currentDate, id);
                } else {//每日余额
                   String maxDay = MryebUtil.getMaxDay(ztgs, gszh);
                    if(DateUtil.compDate(maxDay,yestDate)<1){
                        List<String> dateList = MryebUtil.printDatesBetween(maxDay, yestDate);
                        for (String datestr : dateList) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("rq", datestr);
                            map.put("ztgs", ztgs);
                            map.put("gszh", gszh);
                            map.put("zhxz", zhxz);
                            Map<String, String> mryeMap = MryebUtil.getMrsjye(ztgs, gszh, datestr);//今日收入-今日支出
                            String srje = Util.null2String(mryeMap.get("srje"));
                            String zcje = Util.null2String(mryeMap.get("zcje"));
                            String jrje = MryebUtil.bSubtract(srje, zcje);
                            String zrye = MryebUtil.getMrsjztye(ztgs, gszh, DateUtil.addDate(datestr,-1));
                            map.put("ye", MryebUtil.bAdd(jrje, zrye));
                            map.put("ztye", MryebUtil.isZero(zrye));
                            map.put("srje", MryebUtil.isZero(srje));
                            map.put("zcje", MryebUtil.isZero(zcje));
                            MryebUtil.insertModelData("uf_mryeb", map);
                        }
                    }else {
                        Map<String, String> mryeMap = MryebUtil.getMrsjye(ztgs, gszh, currentDate);//今日收入-今日支出
                        String srje = Util.null2String(mryeMap.get("srje"));
                        String zcje = Util.null2String(mryeMap.get("zcje"));
                        String zrye = MryebUtil.getMrsjztye(ztgs, gszh, yestDate);
                        String jrje = MryebUtil.bSubtract(srje, zcje);
                        Map<String, Object> map = new HashMap<>();
                        map.put("rq", currentDate);
                        map.put("ztgs", ztgs);
                        map.put("gszh", gszh);
                        map.put("zhxz", zhxz);
                        map.put("ye", MryebUtil.bAdd(jrje, zrye));
                        map.put("ztye", MryebUtil.isZero(zrye));
                        map.put("srje", MryebUtil.isZero(srje));
                        map.put("zcje", MryebUtil.isZero(zcje));
                        MryebUtil.insertModelData("uf_mryeb", map);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

